package com.ganee.agora.view

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.ganee.agora.R
import com.ganee.agora.adapter.BookmarkAdapter
import com.ganee.agora.model.InsertActivityEntity
import com.ganee.agora.viewmodel.HomeViewModel
import com.ganee.baselib.arouter.Constance
import com.ganee.baselib.mvvmlibrary.base.BaseActivity
import kotlinx.android.synthetic.main.activity_your_event.*
import kotlinx.android.synthetic.main.toolbar_with_title.*

@Route(path = Constance.ACTIVITY_URL_BOOKMARK)
class BookmarkActivity : BaseActivity<HomeViewModel>() {

    private var mAdapter: BookmarkAdapter? = null
    private val mList = mutableListOf<InsertActivityEntity>()

    private val emptyView: View
        get() {
            val view = LayoutInflater.from(this).inflate(R.layout.common_empty_data, mRecyclerView.parent as ViewGroup, false)
            return view
        }
    override fun getViewModel(): HomeViewModel {
        return ViewModelProviders.of(this).get(HomeViewModel::class.java)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_your_event)
        ARouter.getInstance().inject(this)
        initView()
        initAdapter()
        initRequestData()
    }

    private fun initRequestData() {
        viewModel.getCollectList()
        viewModel.collect.observe(this, Observer<List<InsertActivityEntity>> { list ->
            mList.clear()
            mList.addAll(list)
            mAdapter!!.notifyDataSetChanged()
        })
    }

    private fun initAdapter() {
        mRecyclerView.setHasFixedSize(true)
        mRecyclerView.layoutManager = LinearLayoutManager(this)

        mAdapter = BookmarkAdapter(mList)
        mRecyclerView.addOnItemTouchListener(object : OnItemClickListener() {

            override fun onSimpleItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
                val entity= adapter.getItem(position) as InsertActivityEntity
                ARouter.getInstance().build(Constance.ACTIVITY_URL_HOME_DETAIL).withString("activeId",entity.activeId).navigation()
            }

        })
        mAdapter!!.emptyView = emptyView
        mRecyclerView.adapter = mAdapter
    }

    private fun initView() {
        toolbarCancel.setOnClickListener {
            finish()
        }
        toolbarTitle.text = "Bookmarks"
    }
}
